package day05;

import java.util.Arrays;

//求数组元素的最大值
public class MaxOfArray {
    public static void main(String[] args) {

        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int)(Math.random()*100);
            System.out.println(arr[i]);
        }
        //最大值, 最小值, 总分, 平均分, 总个数---------统计
        //统计的适用场景: 所有数据都填充好了之后进行
        //最大, 最小值算法:
        /*
        int max = arr[0]; //假设第一个元素为最大值
        for (int i = 1; i < arr.length; i++) {//arr[0]作为参照物遍历剩余元素
            if (arr[i] > max) {//判断 i>max(最大值), i<min(最小值)
                max = arr[i];//若剩余的元素大于max,将arr[i]的值赋给max
            }
        }
        System.out.println("最大值为:"+max);
        */

        //                   0  1  2 3
        //假设: int[] arr = {12,56,89,8};
        int max = arr[0]; //假设第一个元素为最大值
        for (int i = 1; i < arr.length; i++) {//arr[0]作为参照物遍历剩余元素
            if (arr[i] > max) {//判断
                max = arr[i];//若剩余的元素大于max,将arr[i]的值赋给max,
             }
        }
        System.out.println("最大值为:"+max); // 12 56 89 8 ---8最大值为:89

        arr = Arrays.copyOf(arr,arr.length+1);//扩容
        arr[arr.length-1] = max; //将最大值max赋值到最后一个元素上
        for(int i = 0; i<arr.length; i++){
            System.out.println(arr[i]);
        }
        /*
        arr = Arrays.copyOf(arr, arr.length+1);
        arr[arr.length-1] = max;
        for(int i = 1; i<arr.length; i++){
            System.out.println(arr[i]);
        }
        * */
        //---------------------------------------
        /*第一遍
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int)(Math.random()*100);
            System.out.println(arr[i]);
        }
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
            System.out.println("最大值为:"+max);
        }

        /* 第二遍
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int)(Math.random()*100);
            System.out.println(arr[i]);
        }
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        System.out.println("最大值为:"+max);

        /*第三遍
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int)(Math.random()*100);
            System.out.println(arr[i]);
        }
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        System.out.println("最大值为:"+max);
        ------------------------------------------
        /*第一遍
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] =(int)(Math.random()*100);
            System.out.println(arr[i]);
        }

        /*第二遍
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int)(Math.random()*100);
            System.out.println(arr[i]);
        }

       /*第三遍
        int[] arr = new int[10];
       for (int i = 0; i < arr.length; i++) {
           arr[i] =(int)(Math.random()*100);
           System.out.println(arr[i]);
       }
       ------------------------------------------
        */
        /*
        int[] arr = new int[10];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = (int)(Math.random()*100);
            System.out.println(arr[i]);
        }*/


    }
}
